﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class ConditonRemarkRepo
    {
        public const string m_msg_head = "ConditionRemark";

        public IEnumerable<ConditonRemarkModels> GetConditionRemark()
        {
            List<ConditonRemarkModels> ConditonRemarks = new List<ConditonRemarkModels>();
            string SQLCommand = @"select TBConditionRemark.*,TBTitleMwwDispenser.TitleMwwDispenserName from TBConditionRemark LEFT JOIN TBTitleMwwDispenser 
                                           ON TBConditionRemark.TitleMwwDispenserId = TBTitleMwwDispenser.TitleMwwDispenserId
                                           WHERE TBConditionRemark.Status <> 'Delete' ORDER BY TBConditionRemark.TitleMwwDispenserId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        ConditonRemarkModels ConditonRemark = new ConditonRemarkModels();
                        ConditonRemark.Id = (int)reader["Id"];
                        ConditonRemark.TitleMwwDispenserId = (int)reader["TitleMwwDispenserId"];
                        ConditonRemark.TitleMwwDispenserName = (string)reader["TitleMwwDispenserName"];
                        ConditonRemark.ConditionRemarkTh = (string)reader["ConditionRemarkTh"];
                        ConditonRemark.ConditionRemarkEng = (string)reader["ConditionRemarkEng"];
                        ConditonRemark.Status = (string)reader["Status"];
                        ConditonRemark.CreateBy = (string)reader["CreateBy"];
                        ConditonRemark.CreateOn = (DateTime)reader["CreateOn"];
                        ConditonRemark.UpdateBy = (string)reader["UpdateBy"];
                        ConditonRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        ConditonRemarks.Add(ConditonRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return ConditonRemarks.ToList();
        }

        public ConditonRemarkModels GetConditionRemarkById(int id)
        {
            ConditonRemarkModels ConditonRemark = new ConditonRemarkModels();
            string SQLCommand = @"select TBConditionRemark.*,TBTitleMwwDispenser.TitleMwwDispenserName from TBConditionRemark LEFT JOIN TBTitleMwwDispenser 
                                           ON TBConditionRemark.TitleMwwDispenserId = TBTitleMwwDispenser.TitleMwwDispenserId where TBConditionRemark.Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        ConditonRemark.Id = (int)reader["Id"];
                        ConditonRemark.TitleMwwDispenserId = (int)reader["TitleMwwDispenserId"];
                        ConditonRemark.TitleMwwDispenserName = (string)reader["TitleMwwDispenserName"];
                        ConditonRemark.ConditionRemarkTh = (string)reader["ConditionRemarkTh"];
                        ConditonRemark.ConditionRemarkEng = (string)reader["ConditionRemarkEng"];
                        ConditonRemark.Status = (string)reader["Status"];
                        ConditonRemark.CreateBy = (string)reader["CreateBy"];
                        ConditonRemark.CreateOn = (DateTime)reader["CreateOn"];
                        ConditonRemark.UpdateBy = (string)reader["UpdateBy"];
                        ConditonRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return ConditonRemark;
        }

        public bool Create(ConditonRemarkModels ConditonRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBConditionRemark
                                    values (@TitleMwwDispenserId,@ConditionRemarkTh,@ConditionRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMwwDispenserId", ConditonRemark.TitleMwwDispenserId);
                    db.AddParameter("@ConditionRemarkTh", ConditonRemark.ConditionRemarkTh);
                    db.AddParameter("@ConditionRemarkEng", ConditonRemark.ConditionRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", ConditonRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(ConditonRemarkModels ConditonRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBConditionRemark 
                                    Set TitleMwwDispenserId=@TitleMwwDispenserId,
                                        ConditionRemarkTh=@ConditionRemarkTh,
                                        ConditionRemarkEng=@ConditionRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", ConditonRemark.Id);
                    db.AddParameter("@TitleMwwDispenserId", ConditonRemark.TitleMwwDispenserId);
                    db.AddParameter("@ConditionRemarkTh", ConditonRemark.ConditionRemarkTh);
                    db.AddParameter("@ConditionRemarkEng", ConditonRemark.ConditionRemarkEng);
                    db.AddParameter("@UserID", ConditonRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(ConditonRemarkModels ConditonRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBConditionRemark
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", ConditonRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID",ConditonRemark.UpdateBy );

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}